#include "catselect.h"
#include "ui_catselect.h"
#include<QMessageBox>
#include<QDebug>
#include<QDesktopWidget>
#include<QtSql/QSqlQuery>
//This class represents option to add category along with list of existing categories to modify them

CatSelect::CatSelect(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CatSelect)
{
    ui->setupUi(this);
    QRect innersize=QApplication::desktop()->availableGeometry(this);
   // ui->listWidget->setGeometry(0,50,(innersize.width()),(innersize.height()));
    ui->scrollArea->setGeometry(0,0,(innersize.width()),(innersize.height()));
    ui->listWidget->setEditFocus(true);
    QAction* backAction = new QAction( tr("Back"), this );
     backAction->setSoftKeyRole( QAction::NegativeSoftKey );
     connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
     addAction( backAction );
     int fontSize=10;
     ui->label->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));
    if(db.db_connect())
    {
        if(db.db_category_table())
        { QSqlQuery qr;

            item1=new QListWidgetItem(tr("Add new category"),ui->listWidget,QListWidgetItem::UserType);
            item1->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));

               qr.prepare("select * from category_table");
               if(qr.exec())
               {

                   while(qr.next())
                   {
                    QString nm=qr.value(0).toString();
                    //qDebug()<<nm<<qr.value(0).toString()<<qr.value(1).toString()<<qr.value(2).toDouble()<<qr.value(3).toDouble();
                    item2=new QListWidgetItem(nm,ui->listWidget,QListWidgetItem::UserType);
                    item2->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));


               }//While
                }//select
           }//Create
        else
            QMessageBox::information(0,"Error !","No category data table exists");
        db.db_close();
    }

}

CatSelect::~CatSelect()
{
    delete ui;
}

void CatSelect::on_listWidget_itemActivated(QListWidgetItem* item)
{
   if(item->text().compare("Add new category")==0)
        (new AddCategory(""))->show();
    else
        (new EditCat(item->text()))->show();
}
void CatSelect::back()
{
    (new expense)->show();
}
